package com.coin.controller;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.coin.exchange.otcbtc.api.OtcbtcAuthApi;
import com.coin.exchange.otcbtc.api.OtcbtcPublicApi;
import com.coin.exchange.otcbtc.vo.CancelOrderRequestVO;
import com.coin.exchange.otcbtc.vo.CancelOrdersRequestVO;
import com.coin.exchange.otcbtc.vo.CreateOrderRequestVO;
import com.coin.exchange.otcbtc.vo.KlinesRequestVO;
import com.coin.exchange.otcbtc.vo.KlinesWithPendingTradesRequestVO;
import com.coin.exchange.otcbtc.vo.ListOrderRequestVO;
import com.coin.exchange.otcbtc.vo.ListOrdersRequestVO;
import com.coin.exchange.otcbtc.vo.MyTradesRequestVO;
import com.coin.exchange.otcbtc.vo.OrderBookRequestVO;
import com.coin.exchange.otcbtc.vo.TradesRequestVO;
import com.coin.exchange.otcbtc.vo.UsersRequestVO;
import com.coin.framework.bean.ResultForm;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/rest/otcbtc")
public class OtcbtcController {

	private static Logger logger = LoggerFactory.getLogger(OtcbtcController.class);
	
	@Autowired
	private OtcbtcPublicApi otcbtcPublicApi;

	@Autowired
	private OtcbtcAuthApi otcbtcAuthApi;

	@ApiOperation(value = "Get all available markets")
	@RequestMapping(value = "/getMarkets", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultForm<?> getMarkets() {
		ResultForm<?> resultForm = null;
		try {
			String res = otcbtcPublicApi.getMarkets();
			JSONArray resultObject = JSONArray.parseArray(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "Get ticker of all markets")
	@RequestMapping(value = "/getTickers", method = RequestMethod.POST)
	public ResultForm<?> getTickers(@RequestBody String marketId) {
		ResultForm<?> resultForm = null;
		try {
//			String marketId = jsonObject.getString("market");
			String res = otcbtcPublicApi.getTickers(marketId);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "Get the order book of specified market")
	@RequestMapping(value = "/getOrderBook", method = RequestMethod.POST)
	public ResultForm<?> getOrderBook(@RequestBody @Valid OrderBookRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
//			String market = jsonObject.getString("market");
//			Integer asksLimit = (Integer) jsonObject.getOrDefault("asks_limit", 20);
//			Integer bidsLimit = (Integer) jsonObject.getOrDefault("bids_limit", 20);
			String res = otcbtcPublicApi.getOrderBook(vo.getMarket(), vo.getAsks_limit(), vo.getBids_limit());
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "Get recent trades on market")
	@RequestMapping(value = "/getTrades", method = RequestMethod.POST)
	public ResultForm<?> getTrades(@RequestBody @Valid TradesRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String market = vo.getMarket();
			Integer limit = vo.getLimit();
			Long timestamp = vo.getTimestamp();
			String from = vo.getFrom();
			String to = vo.getTo();
			String orderBy = vo.getOrder_by();
			String res = otcbtcPublicApi.getTrades(market, limit, timestamp, from, to, orderBy);
			JSONArray resultObject = JSONArray.parseArray(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "Get server current time")
	@RequestMapping(value = "/getTimestamp", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultForm<?> getTimestamp() {
		ResultForm<?> resultForm = null;
		try {
			String res = otcbtcPublicApi.getTimestamp();
			resultForm = ResultForm.createSuccessResultForm(res, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "Get OHLC(k line) of specific market")
	@RequestMapping(value = "/getKlines", method = RequestMethod.POST)
	public ResultForm<?> getKlines(@RequestBody @Valid KlinesRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String market = vo.getMarket();
			Integer limit = vo.getLimit();
			Integer period = vo.getPeriod();
			Long timestamp = vo.getTimestamp();
			String res = otcbtcPublicApi.getKlines(market, limit, period, timestamp);
			JSONArray resultObject = JSONArray.parseArray(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "Get K data with pending trades")
	@RequestMapping(value = "/getKlinesWithPendingTrades", method = RequestMethod.POST)
	public ResultForm<?> getKlinesWithPendingTrades(@RequestBody @Valid KlinesWithPendingTradesRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String market = vo.getMarket();
			String tradeId = vo.getTrade_id();
			Integer limit = vo.getLimit();
			Integer period = vo.getPeriod();
			Long timestamp = vo.getTimestamp();
			String res = otcbtcPublicApi.getKlinesWithPendingTrades(market, tradeId, limit, period, timestamp);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "Get your profile and accounts info")
	@RequestMapping(value = "/getUsers", method = RequestMethod.POST)
	public ResultForm<?> getUsers(@RequestBody @Valid UsersRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String accessKey = vo.getApi_key();
			String apiSecret = vo.getSecret_key();
			String res = otcbtcAuthApi.getUsers(accessKey, apiSecret);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "Get your orders")
	@RequestMapping(value = "/listOrders", method = RequestMethod.POST)
	public ResultForm<?> listOrders(@RequestBody @Valid ListOrdersRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String accessKey = vo.getApi_key();
			String apiSecret = vo.getSecret_key();
			String market = vo.getMarket();
			String state = vo.getState();
			Integer limit = vo.getLimit();
			Integer page = vo.getPage();
			String orderBy = vo.getOrder_by();
			String res = otcbtcAuthApi.listOrders(accessKey, apiSecret, market, state, limit, page, orderBy);
			JSONArray resultObject = JSONArray.parseArray(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "Get information of specified order")
	@RequestMapping(value = "/listOrder", method = RequestMethod.POST)
	public ResultForm<?> listOrder(@RequestBody @Valid ListOrderRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String accessKey = vo.getApi_key();
			String apiSecret = vo.getSecret_key();
			Long orderId = vo.getOrder_id();
			String res = otcbtcAuthApi.listOrder(accessKey, apiSecret, orderId);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "Create a Sell/Buy order")
	@RequestMapping(value = "/createOrder", method = RequestMethod.POST)
	public ResultForm<?> createOrder(@RequestBody @Valid CreateOrderRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String accessKey = vo.getApi_key();
			String apiSecret = vo.getSecret_key();
			String market = vo.getMarket();
			String side = vo.getSide();
			Double volume = vo.getVolume();
			Double price = vo.getPrice();
			String ordType = vo.getOrd_type();
			String res = otcbtcAuthApi.createOrder(accessKey, apiSecret, market, side, volume, price, ordType);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "Cancel an order")
	@RequestMapping(value = "/cancelOrder", method = RequestMethod.POST)
	public ResultForm<?> cancelOrder(@RequestBody @Valid CancelOrderRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String accessKey = vo.getApi_key();
			String apiSecret = vo.getSecret_key();
			Long orderId = vo.getOrder_id();
			String res = otcbtcAuthApi.cancelOrder(accessKey, apiSecret, orderId);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "Cancel all your orders")
	@RequestMapping(value = "/cancelOrders", method = RequestMethod.POST)
	public ResultForm<?> cancelOrders(@RequestBody @Valid CancelOrdersRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String accessKey = vo.getApi_key();
			String apiSecret = vo.getSecret_key();
			String side = vo.getSide();
			String res = otcbtcAuthApi.cancelOrders(accessKey, apiSecret, side);
			JSONArray resultObject = JSONArray.parseArray(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "Get your executed trades")
	@RequestMapping(value = "/getMyTrades", method = RequestMethod.POST)
	public ResultForm<?> getMyTrades(@RequestBody @Valid MyTradesRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String accessKey = vo.getApi_key();
			String apiSecret = vo.getSecret_key();
			String market = vo.getMarket();
			Integer limit = vo.getLimit();
			Long timestamp = vo.getTimestamp();
			String from = vo.getFrom();
			String to = vo.getTo();
			String orderBy = vo.getOrder_by();
			String res = otcbtcAuthApi.getMyTrades(accessKey, apiSecret, market, limit, timestamp, from, to, orderBy);
			JSONArray resultObject = JSONArray.parseArray(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}
	
}
